package gitlet;

import javax.swing.ListSelectionModel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.BorderLayout;
import java.awt.GridLayout;
/**
 * @Description BranchListPanel类是JPanel的子类，用于创建一个显示分支列表的面板。这个面板包含一个滚动面板，滚动面板中包含一个JList来显示分支。当用户选择一个分支时，会添加一个"remove"按钮，点击该按钮会移除选中的分支，并更新面板。
 * @author 陈恒康
 * @date 2023/12
 */
public class BranchListPanel extends JPanel {
    private final Repository repository;
    private final String[] branches;
    private static final JPanel addPanel = new JPanel();
    /**
     * @Description 构造一个新的BranchListPanel对象。该对象包含一个滚动面板，滚动面板中包含一个JList来显示分支。当用户选择一个分支时，会添加一个"remove"按钮，点击该按钮会移除选中的分支，并更新面板。
     * @param repo 与分支列表相关联的仓库对象
     * @param branches 分支列表
     * @author 陈恒康
     * @date 2023/12
     */
    public BranchListPanel(Repository repo, String[] branches) {

        this.branches = branches;
        for (int i = 0; i < branches.length; i++) {
            if (repo.getCurrentBranch().equals(branches[i])) {
                branches[i] = "*" + branches[i];
            }
        }
        // 创建一个JList来显示文件
        JList<String> branchList = new JList<>(branches);
        ListSelectionModel listSelectionModel = branchList.getSelectionModel();
        listSelectionModel.addListSelectionListener(new BranchListPanel.SharedListSelectionHandler());
        // 创建一个滚动面板来包含JList
        JScrollPane scrollPane = new JScrollPane(branchList);
        branchList.setSize(400, 150);
        this.setLayout(new BorderLayout());
        repository = repo;
        this.setLayout(new GridLayout(1, 1));
        // JLabel label = new JLabel("分支");
        // 添加滚动面板到这个面板
        this.add(scrollPane, BorderLayout.CENTER);
    }
    /**
     * @Description SharedListSelectionHandler类实现了ListSelectionListener接口，用于处理用户选择分支的事件。当用户选择一个分支时，会添加一个"remove"按钮，点击该按钮会移除选中的分支，并更新面板。
     * @author 陈恒康
     * @date 2023/12
     */
    class SharedListSelectionHandler implements ListSelectionListener {
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting()) {
                // 如果正在调整，就返回
                return;
            }
            ListSelectionModel lsm = (ListSelectionModel) e.getSource();
            if (!lsm.isSelectionEmpty()) {
                int minIndex = lsm.getMinSelectionIndex();
                int maxIndex = lsm.getMaxSelectionIndex();
                for (int i = minIndex; i <= maxIndex; i++) {
                    if (lsm.isSelectedIndex(i)) {
                        String branch = branches[i];
                        JButton addButton = new JButton("remove");
                        addButton.addActionListener(e1 -> repository.removeBranch(branch));
                        addPanel.removeAll();

                        // 重新验证和重绘面板
                        BranchListPanel.this.revalidate();
                        BranchListPanel.this.repaint();
                    }
                }
            }
        }
    }
}